using System;
using System.Collections.Generic;
using System.Text;

using TTDotNet.Util;

namespace TTDotNet.Engine.Terrain
{
    public class TileCorner
    {
        private Corner corner;
        private Coordinate coord;

        public TileCorner(Corner corner, Coordinate coord)
        {
            this.corner = corner;
            this.coord = coord;
        }

        public Corner Corner
        {
            get { return corner; }
            set { corner = value; }
        }

        public Coordinate Coord
        {
            get { return coord; }
            set { coord = value; }
        }

        public override bool Equals(object obj)
        {
            TileCorner tc2 = (TileCorner)obj;
            return (coord.Equals(tc2.Coord)) && (corner.Direction == tc2.Corner.Direction);
        }

        public static List<TileCorner> SharedTiles(TileCorner tc)
        {
            List<TileCorner> shared = new List<TileCorner>();

            int h = tc.Corner.Height;
            int x = tc.Coord.X;
            int z = tc.Coord.Z;
            Direction dir = tc.Corner.Direction;

            int width = Game.gameWorld.Width;
            int length = Game.gameWorld.Length;

            bool upOK = (z < length - 1);
            bool rightOK = (x < width - 1);
            bool downOK = (z > 0);
            bool leftOK = (x > 0);

            switch (dir)
            {
                case Direction.NE:
                    {
                        if(upOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.SE), new Coordinate(x, z + 1)));
                        if (upOK && rightOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.SW), new Coordinate(x + 1, z + 1)));
                        if (rightOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.NW), new Coordinate(x + 1, z)));
                        break;
                    }
                case Direction.SE:
                    {
                        if(rightOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.SW), new Coordinate(x + 1, z)));
                        if(rightOK && downOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.NW), new Coordinate(x + 1, z - 1)));
                        if(downOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.NE), new Coordinate(x, z - 1)));
                        break;
                    }
                case Direction.SW:
                    {
                        if(downOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.NW), new Coordinate(x, z - 1)));
                        if(downOK && leftOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.NE), new Coordinate(x - 1, z - 1)));
                        if(leftOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.SE), new Coordinate(x - 1, z)));
                        break;
                    }
                case Direction.NW:
                    {
                        if(leftOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.NE), new Coordinate(x - 1, z)));
                        if(leftOK && upOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.SE), new Coordinate(x - 1, z + 1)));
                        if(upOK)
                            shared.Add(new TileCorner(new Corner(h, Direction.SW), new Coordinate(x, z + 1)));
                        break;
                    }
            }

            return shared;
        }

        public static List<TileCorner> GetVertexNeighbours(TileCorner source)
        {
            List<TileCorner> neighbours = new List<TileCorner>();

            int x = source.Coord.X;
            int z = source.Coord.Z;

            Direction dir = source.Corner.Direction;

            if (z < Game.gameWorld.Length - 1)
                neighbours.Add(new TileCorner(new Corner(Game.gameWorld.Map.GetTile(x, z + 1).GetCorner(dir), dir), new Coordinate(x, z + 1)));

            if (x < Game.gameWorld.Width - 1)
                neighbours.Add(new TileCorner(new Corner(Game.gameWorld.Map.GetTile(x + 1, z).GetCorner(dir), dir), new Coordinate(x + 1, z)));

            if (z > 0)
                neighbours.Add(new TileCorner(new Corner(Game.gameWorld.Map.GetTile(x, z - 1).GetCorner(dir), dir), new Coordinate(x, z - 1)));

            if (x > 0)
                neighbours.Add(new TileCorner(new Corner(Game.gameWorld.Map.GetTile(x - 1, z).GetCorner(dir), dir), new Coordinate(x - 1, z)));

            return neighbours;
        }

    }
}
